'use strict'
const articleService = require('service/article')
const response = require('util/response')
const validator = require('util/requestValidator')
const articleValidation = require('validation/article')
module.exports = {
  
  /**
   *  @description: 添加文章
   */
  add: async(ctx, next) => {
    const param = ctx.input.fields || ctx.input
    await validator.validate(
      param,
      articleValidation.add.schema,
      articleValidation.add.options
      )
    const result = await articleService.create(param)
    return response.map(ctx, result)
  },

  /**
   *  @description: 获取文章列表
   */
  list: async(ctx, next) => {
    const result = await articleService.findList(ctx.input)
    return response.map(ctx, result)
  },

  /** 
   * @description: 修改文章
   */
  update: async(ctx, next) => {
    const param = ctx.input
    param.id = ctx.params.id
    await validator.validate(
      param,
      articleValidation.update.schema,
      articleValidation.update.options
    )
    const result = await articleService.update(param)
    return response.map(ctx, result)
  },

  /** 
   * @description: 删除文章
   */
  delete: async(ctx, next) => {
    const param = ctx.input
    param.id = ctx.params.id
    await validator.validate(
      param,
      articleValidation.delete.schema,
      articleValidation.delete.options
    )
    const result = await articleService.delete(param)
    return response.map(ctx, result)
  }
}
